package singsong;

import java.io.File;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;

public class TrackGenerator {

	public static final String TEMP_RECORDING_FILE = "recording.wav";

	private static final float TRACK_SAMPLING_RATE = 100;
	
	private TargetDataLine line;
	
	private SoundRecorder soundRecorder;
	
	public TrackGenerator() {
		AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
				44100.0F, 16, 2, 4, 44100.0F, false);
		DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
		if (!AudioSystem.isLineSupported(info)) {
			System.err.println("Microphone support not found.");
		} else {
			try {
				line = (TargetDataLine) AudioSystem.getLine(info);
				line.open(format);
				soundRecorder = new SoundRecorder(line, new File(TEMP_RECORDING_FILE));
			} catch (LineUnavailableException e) {
				System.err.println("Unable to create microphone input.");
			}
		}
	}

	public void record() {
		soundRecorder.start();
	}

	public Track done() {
		soundRecorder.stop();
		return new Track("New Track", new TrackDataBuilder(TEMP_RECORDING_FILE, TRACK_SAMPLING_RATE, 0).build());
	}
}
